package com.ywq.app.biz.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.ywq.app.biz.ArtActivityBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.dao.ArtActivityDao;
import com.ywq.app.domain.ArtActivity;
import com.ywq.app.domain.ArtActivityMember;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;

public class ArtActivityBizImpl implements ArtActivityBiz {

	private final static Logger logger = LoggerFactory
			.getLogger(ArtRoomBizImpl.class);
	@Autowired
	private IdentifierBiz idBiz;
	
	private ArtActivityDao artActivityDao;

	public void setArtActivityDao(ArtActivityDao artActivityDao) {
		this.artActivityDao = artActivityDao;
	}
	@Override
	public PageModel getArtActivityListForMisc(Integer page,Integer pageSize)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		int count = artActivityDao.getArtActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artActivityDao.getArtActivityList(map));
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1)
				/ pageSize);
		return pm;
	}
	
	@Override
	public PageModel getArtActivityList(Integer page,String type)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("activityType", type);
		int count = artActivityDao.getArtActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivity> actList=artActivityDao.getArtActivityList(map);
		actList=formatTime(actList);
		pm.setList(actList);
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)
				/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	@Override
	public PageModel getArtActivityListOfDate(Integer page,String st,String ed) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		map.put("start", st);
		map.put("end", ed);
		int count = artActivityDao.getArtActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivity> actList=artActivityDao.getArtActivityList(map);
		actList=formatTime(actList);
		pm.setList(actList);
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)
				/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	//周
	@Override
	public PageModel getArtAactivtyWeek(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar currentDate = Calendar.getInstance();
		int weekday=currentDate.get(7)-1;
		currentDate.add(5, -weekday);
		Date start=currentDate.getTime();
		String st =f.format(start);
		currentDate.add(5, 6);
		Date end =currentDate.getTime();
		String ed =f.format(end);
		map.put("start", st);
		map.put("end", ed);
		int count = artActivityDao.getArtActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivity> actList=artActivityDao.getArtActivityList(map);
		actList=formatTime(actList);
		pm.setList(actList);
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)
				/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	//月
	@Override
	public PageModel getArtAactivtyMonth(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_MADE);
		map.put("pageSize", PageSize.PAGE_SIZE_MADE);
		 Calendar cal = Calendar.getInstance();
		 int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		map.put("month", month);
		map.put("month", year);
		int count = artActivityDao.getArtActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivity> actList=artActivityDao.getArtActivityList(map);
		actList=formatTime(actList);
		pm.setList(actList);
		pm.setPageSize(PageSize.PAGE_SIZE_MADE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_MADE - 1)
				/ PageSize.PAGE_SIZE_MADE);
		return pm;
	}
	@Override
	public void insertArtActivity(ArtActivity artActivity)
			throws AppBusinessException {
		//String id = IdGenerator.createIdentifier();
		String id = idBiz.generateId("art_activity");
		artActivity.setIdentifier(id);
		if("".equals(artActivity.getStartTime())|| "".equals(artActivity.getEndTime())){
			artActivity.setStartTime("0");
			artActivity.setEndTime("0");
		}
		artActivityDao.insertArtActivity(artActivity);
	}

	@Override
	public void updateArtActivity(ArtActivity artActivity) throws AppBusinessException {
		if("".equals(artActivity.getStartTime())|| "".equals(artActivity.getEndTime())){
			artActivity.setStartTime("0");
			artActivity.setEndTime("0");
		}
		artActivityDao.updateArtActivity(artActivity);
		
	}
	@Override
	public void deleteActivity(String identifier) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", identifier);
		artActivityDao.deleteActivity(map);
		
	}
	
	@Override
	public List<ArtActivity> getOldActivityList() throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		return artActivityDao.getOldActivityList(map);

	}

	@Override
	public List<ArtActivity> getNowActivityList() throws AppBusinessException {
		List<ArtActivity> nowList = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		nowList = artActivityDao.getNowActivityList(map);
		if (nowList.size() > 0) {
			nowList=formatTime(nowList);
		}
	return nowList;
	}

	@Override
	public List<ArtActivity> getOldActivityListBySize(Integer size)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		map.put("pageSize", size);
		map.put("startId", 0);
		return artActivityDao.getOldActivityList(map);
	}

	@Override
	public PageModel getRBackList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_ART_OLD);
		map.put("pageSize", PageSize.PAGE_ART_OLD);
		map.put("isShow", "true");
		int count = artActivityDao.getOldActivityListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivity> oldList =artActivityDao.getOldActivityList(map);
		oldList=formatTime(oldList);
		pm.setList(oldList);
		pm.setPageSize(PageSize.PAGE_ART_OLD);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_ART_OLD - 1)
				/ PageSize.PAGE_ART_OLD);
		return pm;
	}

	@Override
	public List<ArtActivity> formatTime(List<ArtActivity> rList)
			throws AppBusinessException {
		try {
			for (ArtActivity art : rList) {
				SimpleDateFormat secData = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat secTime = new SimpleDateFormat("HH:mm:ss");
				Date sDate = secData.parse(art.getStartDate());
				Date eDate = secData.parse(art.getEndDate());
				if("0".equals(art.getStartTime())|| "0".equals(art.getEndTime())){
					
				}else{
					Date sTime = secTime.parse(art.getStartTime());
					Date eTime = secTime.parse(art.getEndTime());
					art.setsTime(sTime);
					art.seteTime(eTime);
				}
				art.setsDate(sDate);
				art.seteDate(eDate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rList;
	}
	@Override
	public ArtActivity formatTimeOne(ArtActivity art) throws AppBusinessException {
		try {
			
				SimpleDateFormat secData = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat secTime = new SimpleDateFormat("HH:mm:ss");
				Date sDate = secData.parse(art.getStartDate());
				Date eDate = secData.parse(art.getEndDate());
				if("0".equals(art.getStartTime())|| "0".equals(art.getEndTime())){
					
				}else{
					Date sTime = secTime.parse(art.getStartTime());
					Date eTime = secTime.parse(art.getEndTime());
					art.setsTime(sTime);
					art.seteTime(eTime);
				}
				art.setsDate(sDate);
				art.seteDate(eDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return art;
	}
	
	@Override
	public ArtActivity getArtActivityById(String id)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		map.put("isShow", "true");
		ArtActivity artActivity =artActivityDao.getArtActivityById(map);
		artActivity =formatTimeOne(artActivity);
		return artActivity;
	}
	// 参加活动
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public Boolean signupmember(ArtActivityMember artActivityMember) throws AppBusinessException {
		//查询活动是否可以继续参加
		ArtActivity artActivity =getArtActivityById(artActivityMember.getActivityId());
		if(artActivity!=null){
			int pnum=artActivity.getPersonNum();
			int signNum=artActivity.getSignNum();
			if(signNum<pnum){
				//可以参加
				artActivity.setSignNum(signNum+1);
				artActivityDao.updateArtActivity(artActivity);
				//增加关联表
				artActivityDao.insertArtActivityMember(artActivityMember);
				return  true;
			}else{
				return  false;
			}
		}else{
			return  false;
		}
	}
	@Override
	public PageModel artPersonList(Integer page ,Integer pagesize,String activityId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pagesize);
		map.put("pageSize", pagesize);
		map.put("activityId", activityId);
		int count = artActivityDao.artPersonListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtActivityMember> oldList =artActivityDao.artPersonList(map);
		pm.setList(oldList);
		pm.setPageSize(pagesize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pagesize - 1) / pagesize);
		return pm;
	}
	
	
}
